<?php

declare(strict_types=1);


namespace app\index\controller;

use think\Request;
use think\admin\Controller;
use think\exception\HttpResponseException;
use think\facade\Session;
use think\facade\Cache;
use think\facade\Db;
#use app\api\libs\Jwtauth;
use think\facade\Config;
#use app\BaseController;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Exchange\AMQPExchangeType;
use PhpAmqpLib\Message\AMQPMessage;
use PhpAmqpLib\Wire\AMQPTable;


//生产者
class Mq extends Controller
{

 //初始化
    protected function initialize()
    {
        //$this->table= 'system_originalarticle';
        $this->id = Session::get('fans_id');
       
        $this->MqConfig = Config::get('mq');
    }
 
    public function index()
    {
        //halt("xuanranccc");
        //halt($this->config['fileSize']);
        // halt($this->config['fileext']);
        //$this->fetch();
        exit("这里是mq");
        //$this->redirect(sysuri('admin/login/index'));
    }

    /* 
    *  @desc      方法说明
    *  @author    saruri <saruri@163.com>
    *  @date      2020/08/01 18:05:35  
    */ 
    public function  conn()
    {
        //do somehing
       // halt($this->MqConfig);
       //echo phpinfo();
      // exit();
        $connection= new AMQPStreamConnection($this->MqConfig['ip'], $this->MqConfig['port'], $this->MqConfig['user'], $this->MqConfig['pwd'], $this->MqConfig['vhost']);
        return $connection;
    }



    /*
    *  @desc      发送队列
    *  @author    saruri <saruri@163.com>
    *  @date      2020/08/01 18:02:52
    */
    public function send(array $arr)
    {
        //队列名  消息队列载体，每个消息都会被投入到一个或多个队列。
        $queue = 'hello';
        $routing =$queue;
        //建立连接
        $connection = self::conn();//new AMQPStreamConnection('127.0.0.1', 5672, 'zq', '123456', '/');
        //获取信道
        $channel = $connection->channel();

        //声明创建队列
        $channel->queue_declare($queue, false, false, false, false);

        for ($i=0; $i < 3; ++$i) {
            //sleep(2);//休眠1秒
            //消息内容
            $messageBody = "saruri hello Now Time:".date("h:i:s");
            //将我们需要的消息标记为持久化 - 通过设置AMQPMessage的参数delivery_mode = AMQPMessage::DELIVERY_MODE_PERSISTENT
            $message = new AMQPMessage($messageBody, array('content_type' => 'text/plain', 'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT));
            //发送消息
            $channel->basic_publish($message, '', $routing);
            echo "Send Message:". $i."\n";
        }

        //关闭信道
        $channel->close();
        //关闭连接
        $connection->close();
        return 'Send Success';
    }



    /**
    * 接收 处理 前台传输的数据 
    * @author    saruri <saruri@163.com>
    * @date      2021/03/01 09:59:32  
    * @auth true
    * @menu true
    * @login true
    * @throws \think\Exception
    * @throws \think\db\exception\DataNotFoundException
    * @throws \think\db\exception\DbException
    * @throws \think\db\exception\ModelNotFoundException
    */ 
    public function  GetFront()
    {
        //do somehing
        //exit("这里是:");

        //接收参数
    
        $re=input();
    }
}
